package org.easy.file.monitor;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.easy.file.handler.FileParse;
import org.easy.file.handler.FileQueue;
import org.easy.file.handler.IFileParse;
import org.easy.tool.date.DateUtil;

import java.io.File;

/**
 * @Description: 文件监听类
 * @Author: wxl
 * @CreateTime: 2022/12/27 10:42
 */
@Slf4j
public class FileListener extends FileAlterationListenerAdaptor {

    private FileQueue fileQueue;

    public FileListener () {
        this.fileQueue = new FileQueue();
    }

    @Override
    public void onFileCreate(File file) {
        log.info("于[{}]创建文件[{}]:", DateUtil.localDateTime(),file.getName());
        FileParse fileParse = new IFileParse(file);
        getFileQueue().addFileParse(fileParse);
    }

    @Override
    public void onFileDelete(File file) {
        log.info("于[{}]删除文件[{}]:", DateUtil.localDateTime(),file.getName());
    }

    @Override
    public void onFileChange(File file) {
        log.info("于[{}]文件变化[{}]:", DateUtil.localDateTime(),file.getName());
    }

    public FileQueue getFileQueue() {
        return fileQueue;
    }

    public void setFileQueue(FileQueue fileQueue) {
        this.fileQueue = fileQueue;
    }
}
